//
//  ShopInfoViewController.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/11/1.
//

#import "ShopInfoViewController.h"
#import <ImageIO/ImageIO.h>
#import "ShopModel.h"
#import "GoodsInfoTableView.h"
#import "GoodsInfoHeaderView.h"
#import "GoodsInfoBottomView.h"
#import "PublishShopViewController.h"
#import "VideoPlayViewController.h"
#import "BaseNaviViewController.h"
#import <GKPhotoBrowser/GKPhotoBrowser.h>
#import "GoodsInfoImageTableViewCell.h"
#import "UpdateTipsView.h"

@interface ShopInfoViewController ()<GKPhotoBrowserDelegate>

@property (nonatomic, strong) GoodsInfoTableView *tableview;
@property (nonatomic, strong) ShopModel *model;
@property (nonatomic, strong) GoodsInfoHeaderView *headerView;
@property (nonatomic, strong) GoodsInfoBottomView *bottomView;
@property (nonatomic, weak) GKPhotoBrowser *browser;

@end

@implementation ShopInfoViewController

- (void)initDidLoad {
    [UIViewController removeControllerWithString:@"ScanCodeViewController"];
    self.hbd_barAlpha = 1;
    self.navigationTitle = @"商品详情";
}

- (void)initViews {
    [self.view addSubview:self.headerView];
    [self.view addSubview:self.tableview];
    [self.view addSubview:self.bottomView];
    
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(0);
        make.top.mas_equalTo(20);
        make.height.mas_equalTo(40);
    }];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headerView.mas_bottom).offset(10);
        make.leading.trailing.mas_equalTo(0);
        make.bottom.mas_equalTo(-44-kHeightForTabbarExtra);
    }];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.mas_equalTo(0);
        make.height.mas_equalTo(44+kHeightForTabbarExtra);
    }];
}


#pragma mark - data
- (void)initRequestData {
    [YPProgressHud showHudInview:self.view];
    [NET getGoodsInfoWithGoodsId:self.goodsId success:^(id  _Nonnull responseObject) {
        [YPProgressHud hideHudForView:self.view];
        self.model = [ShopModel mj_objectWithKeyValues:responseObject];
        self.headerView.model = self.model;
        self.bottomView.model = self.model;
        self.tableview.model = self.model;
        [self.tableview reloadData];
    } failure:^(NSError * _Nonnull error) {
        [YPProgressHud hideHudForView:self.view];
    }];
}

/// 编辑商品
- (void)editShopInfo:(ShopModel *)model {
    PublishShopViewController *vc = [[PublishShopViewController alloc] init];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}

/// 删除
- (void)deleteShop:(ShopModel *)model {
    [UIAlertController showAlertInViewController:self withTitle:@"确定要删除该商品吗?" message:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"删除" otherButtonTitles:@[] tapBlock:^(UIAlertAction * _Nonnull action, NSInteger buttonIndex) {
        if (buttonIndex == UIAlertControllerBlocksDestructiveButtonIndex) {
            [YPProgressHud showHudInview:self.view];
            [NET deleteShopWithGoodsId:model.goodsId success:^(id  _Nonnull responseObject) {
                [YPProgressHud hideHudForView:self.view];
                [self.navigationController popViewControllerAnimated:YES];
                [self.tableview reloadData];
            } failure:^(NSError * _Nonnull error) {
                [YPProgressHud hideHudForView:self.view];
                kShowText(error.userInfo[kHttpErrorReason]);
            }];
        }
    }];
}

/// 上下架
- (void)topOrDownShop:(ShopModel *)model {
    [YPProgressHud showHudInview:self.view];
    [NET topOrDownShopWithGoodsId:model.goodsId enable:!model.enabled success:^(id  _Nonnull responseObject) {
        [YPProgressHud hideHudForView:self.view];
        model.enabled = !model.enabled;
        self.bottomView.model = model;
    } failure:^(NSError * _Nonnull error) {
        [YPProgressHud hideHudForView:self.view];
        kShowText(error.userInfo[kHttpErrorReason]);
    }];
}

/// 置顶
- (void)topShop:(ShopModel *)model {
    [UIAlertController showAlertInViewController:self withTitle:model.top ? @"确认取消置顶商品?" : @"确认置顶商品?" message:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@[] tapBlock:^(UIAlertAction * _Nonnull action, NSInteger buttonIndex) {
        [YPProgressHud showHudInview:self.view];
        [NET topSetShopWithGoodsId:model.goodsId top:!model.top success:^(id  _Nonnull responseObject) {
            model.top = !model.top;
            self.bottomView.model = model;
            [YPProgressHud hideHudForView:self.view];
        } failure:^(NSError * _Nonnull error) {
            [YPProgressHud hideHudForView:self.view];
            kShowText(error.userInfo[kHttpErrorReason]);
        }];
    }];
}

/// 售罄
- (void)soldOutData:(ShopModel *)model {
    [YPProgressHud showHudInview:self.view];
    [NET soldOutWithGoodsId:model.goodsId success:^(id  _Nonnull responseObject) {
        kShowText(@"更新完成");
        [YPProgressHud hideHudForView:self.view];
    } failure:^(NSError * _Nonnull error) {
        [YPProgressHud hideHudForView:self.view];
        kShowText(error.userInfo[kHttpErrorReason]);
    }];
}

/// 展示更新弹窗
- (void)showUpdateTipView:(ShopModel *)model {
    UpdateTipsView *v = [[UpdateTipsView alloc] init];
    [v show];
    @weakify(self)
    [v setUpdateShopBlock:^{
     @strongify(self)
        [self updateGoods:model];
    }];
    [v setCheckContentBlock:^{
       @strongify(self)
        ShopInfoViewController *vc = [[ShopInfoViewController alloc] init];
        vc.goodsId = model.goodsId;
        [self.navigationController pushViewController:vc animated:YES];
    }];
}

/// 同步更新
- (void)updateGoods:(ShopModel *)model {
    [YPProgressHud showHudInview:self.view];
    [NET updateGoodsWithGoodsId:model.goodsId success:^(id  _Nonnull responseObject) {
        kShowText(@"同步完成");
        [YPProgressHud hideHudForView:self.view];
    } failure:^(NSError * _Nonnull error) {
        [YPProgressHud hideHudForView:self.view];
    }];
}

/// 一键转发商品
- (void)forwardGoods:(ShopModel *)model {
    [YPProgressHud showHudInview:self.view];
    [NET forwardGoodsWithGoodsId:model.goodsId success:^(id  _Nonnull responseObject) {
        kShowText(@"转发成功");
        [YPProgressHud hideHudForView:self.view];
    } failure:^(NSError * _Nonnull error) {
        [YPProgressHud hideHudForView:self.view];
        kShowText(error.userInfo[kHttpErrorReason]);
    }];
}

#pragma mark - 懒加载
- (GoodsInfoTableView *)tableview {
    if (!_tableview) {
        _tableview = [[GoodsInfoTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        @weakify(self)
        [_tableview.subject subscribeNext:^(NSIndexPath *x) {
         @strongify(self)
            if (x.row == 0) {
                return;
            }
            GoodsInfoImageTableViewCell *cell = [self.tableview cellForRowAtIndexPath:x];
            NSMutableArray *photos = [NSMutableArray array];
            NSMutableArray *arr = [NSMutableArray array];
            if (self.model.video.length > 0) {
                [arr addObject:self.model.videoPicUrl];
            }
            if (self.model.imgUrl.count > 0) {
                [arr addObjectsFromArray:self.model.imgUrl];
            }
            [arr enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                GKPhoto *p = [[GKPhoto alloc] init];
                p.url = [NSURL URLWithString:obj];
                p.sourceImageView = cell.imageV;
                if (self.model.video.length > 0 && x.row == 1 && idx == 0) {
                    p.videoUrl = [NSURL URLWithString:self.model.video];
                }
                [photos addObject:p];
            }];
            GKPhotoBrowser *browser = [GKPhotoBrowser photoBrowserWithPhotos:photos currentIndex:x.row-1];
            browser.showStyle = GKPhotoBrowserShowStyleZoom;
            browser.hideStyle = GKPhotoBrowserHideStyleZoomScale;
            browser.loadStyle = GKPhotoBrowserLoadStyleIndeterminateMask;
            browser.maxZoomScale = 20.0f;
            browser.doubleZoomScale = 2.0f;
            browser.isAdaptiveSafeArea = YES;
            browser.hidesCountLabel = YES;
            browser.hidesSavedBtn = YES;
            browser.isFullWidthForLandScape = NO;
            browser.isSingleTapDisabled = YES;
            browser.showPlayImage = NO;
            browser.isVideoReplay = YES;
            browser.videoPlayImage = [UIImage imageNamed:@"shopinfo_play"];
            browser.delegate = self;
            [browser showFromVC:self];
            self.browser = browser;
        }];
    }
    return _tableview;
}

- (GoodsInfoHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[GoodsInfoHeaderView alloc] init];
    }
    return _headerView;
}

- (GoodsInfoBottomView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[GoodsInfoBottomView alloc] init];
        @weakify(self)
        [_bottomView setEditBlock:^(ShopModel * _Nonnull model) {
         @strongify(self)
            [self editShopInfo:model];
        }];
        [_bottomView setTopDownBlock:^(ShopModel * _Nonnull model) {
           @strongify(self)
            [self topOrDownShop:model];
        }];
        [_bottomView setTopBlock:^(ShopModel * _Nonnull model) {
           @strongify(self)
            [self topShop:model];
        }];
        [_bottomView setDeleteBlock:^(ShopModel * _Nonnull model) {
           @strongify(self)
            [self deleteShop:model];
        }];
        [_bottomView setShouqingBlock:^(ShopModel * _Nonnull model) {
            @strongify(self)
            [self soldOutData:model];
        }];
        [_bottomView setUpdateBlock:^(ShopModel * _Nonnull model) {
            @strongify(self)
            [self showUpdateTipView:model];
        }];
        [_bottomView setEditTransBlock:^(ShopModel * _Nonnull model) {
            @strongify(self)
            if (!self.isFollow) {
                kShowText(@"请先关注商家");
                return;
            }
            [self editShopInfo:model];
        }];
        [_bottomView setTransBlock:^(ShopModel * _Nonnull model) {
            @strongify(self)
            if (!self.isFollow) {
                kShowText(@"请先关注商家");
                return;
            }
            [self forwardGoods:model];
        }];
    }
    return _bottomView;
}

- (void)photoBrowser:(GKPhotoBrowser *)browser singleTapWithIndex:(NSInteger)index {
    [browser dismiss];
}
- (void)photoBrowser:(GKPhotoBrowser *)browser willLayoutSubViews:(NSInteger)index {
//    [GKCover layoutSubViews];
}
- (void)photoBrowser:(GKPhotoBrowser *)browser didChangedIndex:(NSInteger)index {
    
}


@end
